import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/utils/toast_utils.dart';

///
/// @DIR_PATH：lib/utils
/// @TIME：2023/11/10 9:52
/// @AUTHOR：starr
/// 路由组件封装
class NavigatorUtils {
  ///关闭当前页面
  ///[context]当前页面的context
  ///[parameters]需要回传到上一个页面的参数
  static pop(BuildContext context, {parameters}) {
    if (Navigator.canPop((context))) {
      Navigator.of(context).pop(parameters);
    } else {
      //最后一个页面不可以pop
      ToastUtils.showToast("当前页面不可以关闭");
    }
  }

  ///动态路由方法封装
  ///[context]当前页面的Context
  ///[routeName]目标页面的路由名称
  ///[parameters]向目标页面传的参数
  ///[callback]目标页面关闭时的回调函数
  ///[isReplace]是否替换当前的路由
  static pushPage(
    BuildContext context,
    Widget page, {
    String? routeName,
    parameters,
    Function? callback,
    bool isReplace = false,
  }) {
    PageRoute pageRoute;
    //ios平台
    if (Platform.isIOS) {
      pageRoute = CupertinoPageRoute(
        builder: (_) {
          return page;
        },
        settings: RouteSettings(name: routeName ?? "", arguments: parameters),
      );
    } else {
      //android等其他平台使用Material风格
      pageRoute = MaterialPageRoute(
        builder: (_) {
          return page;
        },
        settings: RouteSettings(name: routeName ?? "", arguments: parameters),
      );
    }

    //替换当前的路由
    //目标页面关闭时回调函数与回传参数
    if (isReplace) {
      Navigator.of(context)
          .pushReplacement(pageRoute)
          .then((value) => {callback?.call(value)});
    }
    //压栈
    //目标页面关闭时回调函数与回传参数
    Navigator.of(context).push(pageRoute).then((value) => {callback?.call(value)});
  }

  ///以透明过渡的方式打开新的页面
  ///[opaque] 是否以背景透明的方式打开新的页面
  ///[isReplace] 是否替换当前路由中的页面
  ///[mills]透明过度页面打开的时间
  ///[endMills]页面关闭的时间
  ///[isBuilder]透明过渡动画的构建模式
  static void openPageByFade(BuildContext context, Widget page,
      {bool isReplace = false,
      bool opaque = true,
      int mills = 800,
      int endMills = 400,
      bool isBuilder = false,
      Function(dynamic value)? dismissCallBack}) {
    //创建自定义路由 PageRouteBuilder
    PageRouteBuilder pageRouteBuilder =
        buildRoute1(mills, endMills, page, opaque);
    //是否替换当前显示的Page
    if (isReplace) {
      Navigator.of(context).pushReplacement(pageRouteBuilder).then(
        (value) {
          if (dismissCallBack != null) {
            dismissCallBack(value);
          }
        },
      );
    } else {
      Navigator.of(context).push(pageRouteBuilder).then(
        (value) {
          if (dismissCallBack != null) {
            dismissCallBack(value);
          }
        },
      );
    }
  }

  ///自定义路由构建方式一
  ///透明渐变
  static PageRouteBuilder buildRoute1(
      int mills, int endMills, Widget page, bool opaque) {
    return PageRouteBuilder(
      //值为false时以透明背景方式打开
      opaque: opaque,
      pageBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        //目标页面
        return page;
      },
      //动画时间
      transitionDuration: Duration(milliseconds: mills),
      reverseTransitionDuration: Duration(milliseconds: endMills),
      //过渡动画
      transitionsBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        Widget child,
      ) {
        //渐变过渡动画
        return FadeTransition(
          // 透明度从 0.0-1.0
          opacity: Tween(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(
              parent: animation,
              //动画曲线规则，这里使用的是先快后慢
              curve: Curves.fastOutSlowIn,
            ),
          ),
          child: child,
        );
      },
    );
  }
}
